package com.aaa.system.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.aaa.common.pojo.User;
import com.aaa.system.dao.UserDao;
import com.aaa.system.service.UserService;
import com.aaa.system.vo.UserVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

/**
 * @program: education-parent
 * @description:
 * @author: 闫克起2
 * @create: 2023-05-22 16:25
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Override
    public User findByNameAndPwd(String username, String password) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        //把输入的密码加盐
        wrapper.eq("password", DigestUtil.sha256Hex(password));
        User user = userDao.selectOne(wrapper);
        return user;
    }

    //注意: 分页效果要想出现必须分页的配置类
    @Override
    public Page<User> findByConditionPage(Integer current, Integer pageSize, UserVo userVo) {
        Page<User> page=new Page<>(current,pageSize);
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        if(StringUtils.hasText(userVo.getRealName())){
            wrapper.like("nick_name",userVo.getRealName());
        }
        Page<User> page1 = userDao.selectPage(page, wrapper);
        return page1;
    }

    @Override
    public int updateStatus(String id, Integer status) {
        User user=new User();
        user.setId(id);
        user.setIsDeleted(status);
        user.setGmtModified(LocalDateTime.now()); //JDK1.8---->Date线程安全问题--LocalDate LocalDateTime
        int row = userDao.updateById(user);
        return row;
    }


}








